home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d1 / bastips1.arc / SCRNSWAP.TXT < prev    next >
Text File  |  1988-11-29  |  8KB  |  200 lines

  1.                        IBM Screen Swapping
  2.       (COMPUTE! Magazine February 1986 by Paul W. Carlson)
  3.  
  4.      You can achieve many interesting effects, including animation, by
  5. rapidly switching between several graphics screens stored in memory.
  6. This capability isn't a standard feature of the IBM PC.  With help
  7. from two short machine language subroutines, however, you can write
  8. programs that swap screens almost instantly.  The subroutines copy the
  9. video bitmap to or from an array in about five thousandths of a second,
  10. much too fast for the eye to see.  In fact, this is even faster than
  11. the video monitor can display a frame, so the effect is instantaneous.
  12.  
  13.      To get started, type in Program 1 below.  It creates two files,
  14. SCRNARRY.BAS and ARRYSCRN.BAS, which contain the two machine language
  15. subroutines.  The first routine copies the video bitmap to an array,
  16. and the second copies the contents of an array to the video bitmap.
  17. The routines achieve their speed by treating the bitmap as a continuous
  18. string of 16,192 bytes.
  19.  
  20.      For an example of how to use these routines in your own programs,
  21. type in Program 2 and save it on the same disk with SCRNARRY.BAS and
  22. ARRYSCRN.BAS.  Before running Program 2, make sure the disk is in the
  23. active drive; it accesses the two routines as it runs.  After typing
  24. RUN, don't press any keys until you want to halt the program.
  25.  
  26.      You should see three multicolored spirals on the screen.  The
  27. first two disappear as soon as they're completed, and the third seems
  28. to rotate.  The rotation, of course, is an illusion.  Here's what
  29. happens: In the split-second between the time the first two spirals
  30. are completed and then erased, each screen is copied into an array by
  31. SCRNARRY.BAS.  The third spiral is also copied into an array.  Finally,
  32. the contents of all three arrays are repeatedly copied to the screen by
  33. ARRYSCRN.BAS to get the rotating effect. Actually, the program requires
  34. a time-delay loop to keep the screen-flipping from happening too fast.
  35.  
  36.      You can load a graphics screen from disk directly into an array
  37. the same way Program 2 loads the machine language into arrays.  Why
  38. would you want to do this?  Suppose you had saved graphics screens
  39. from three different programs on disk using statements such as this:
  40.  
  41. DEF SEG=&HB800:BSAVE"filename",0,16192
  42.  
  43. with filenames of PIC1, PIC2 and PIC3.  You could then use Program 3
  44. to display a "slide show" of your creations.
  45.  
  46.     This interesting program displays one screen while loading another.
  47. Pressing the space bar (after giving the next screen time to load)
  48. displays the next picture.  The program could be extended to accomodate
  49. any number of screens, even prompting you to change disks if necessary.
  50. It needs only one array to store the screens no matter how many you
  51. want to display, since it stores only one screen at any moment.
  52.  
  53.      Notice tha the statement LA=0 in line 10 of Program 3 prevents the
  54. address of the ATOS array from changing after it is assigned a value
  55. for PUTSCRN in line 30.
  56.  
  57. Program 1: Screen Swapping Routines
  58.  
  59. 10 DIM M(7),J(6):DEF SEG
  60. 20 FOR N=0 TO 26:READ B
  61. 30 POKE VARPTR(M(0))+N,B:NEXT
  62. 40 BSAVE"SCRNARRY",VARPTR(M(0)),27
  63. 50 FOR N=0 TO 22:READ B
  64. 60 POKE VARPTR(J(0))+N,B:NEXT
  65. 70 BSAVE"ARRYSCRN",VARPTR(J(0)),23:END
  66. 80 DATA 6,30,7,30,139,236,184,0
  67. 90 DATA 184,142,216,185,160,31,51,246
  68. 100 DATA 139,126,8,252,243,165,31,7
  69. 110 DATA 202,2,0,6,139,236,184,0
  70. 120 DATA 184,142,192,185,160,31,51,255
  71. 130 DATA 139,118,6,252,243,165,7,202
  72. 140 DATA 2,0
  73.  
  74. Program 2: Spiral Demo
  75.  
  76. 10 DIM SCRN1(4048),SCRN2(4048),SCRN3(4048),STOA(7),ATOS(6)
  77. 20 DEF SEG:BLOAD"SCRNARRY",VARPTR(STOA(0))
  78. 30 BLOAD"ARRYSCRN",VARPTR(ATOS(0))
  79. 40 KEY OFF:SCREEN 1:COLOR 0,0
  80. 50 FOR C=1 TO 3:W=C:CLS
  81. 60 TP=6.283185:F=80/TP:DA=TP/9:DB=TP/20:A=0
  82. 70 FOR I=1 TO 9:B=0:A=A+DA:PSET(160,100)
  83. 80 FOR J=1 TO 20:B=B+DB:R=F*B
  84. 90 X=160+1.2*R*SIN(A+B):Y=100+R*COS(A+B)
  85. 100 LINE -(X,Y),3:NEXT J,I
  86. 110 CIRCLE(160,100),96,3:A=DA/2
  87. 120 FOR I=1 TO 9:A=A+DA
  88. 130 X=160+1.18*R*SIN(A):Y=100+.96*R*COS(A)
  89. 140 C=C MOD 3+1:PAINT(X,Y),C,3:NEXT I
  90. 150 GETSCRN=VARPTR(STOA(0))
  91. 160 ON W GOTO 170,180,190
  92. 170 CALL GETSCRN(SCRN1(0)):GOTO 200
  93. 180 CALL GETSCRN(SCRN2(0)):GOTO 200
  94. 190 CALL GETSCRN(SCRN3(0))
  95. 200 NEXT C
  96. 210 PUTSCRN=VARPTR(ATOS(0))
  97. 220 CALL PUTSCRN(SCRN1(0)):FOR J=0 TO 100:NEXT
  98. 230 CALL PUTSCRN(SCRN2(0)):FOR J=0 TO 100:NEXT
  99. 240 CALL PUTSCRN(SCRN3(0)):FOR J=0 TO 100:NEXT
  100. 250 IF INKEY$="" THEN 220
  101. 260 CLS:SCREEN 0:WIDTH 80:KEY ON:END
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111. Program 2 Explanation
  112.  
  113. Line     Description
  114. 20,30    Loads the machine language subroutines into the STOA and
  115.            ATOS arrays
  116. 40-140   Draws and paints three spirals, each with the colors shifted
  117. 150      GETSCRN is the entry point for the subroutine that copies the
  118.            screen to an array.  No new simple variables can be assigned
  119.            from the point GETSCRN is computed to the point it is used
  120.            in a CALL statement. Assigning simple variables causes array
  121.            address to move.
  122. 160-200  Copies the screen to array SCRN1, SCRN2 or SCRN3 after each
  123.            spiral is complete.
  124. 210      PUTSCRN is the entry point for the subroutine that copies an
  125.            array to the screen.  The same note for line 150 applies
  126.            here also.
  127. 220-250  Repeatedly copies the arrays SCRN1, SCRN2 and SCRN3 to the
  128.            screen until a key is pressed.
  129.  
  130. Program 3: Slide Show Demo
  131.  
  132. 10 DIM SCRN(4048),ATOS(6):LA=0
  133. 20 DEF SEG:BLOAD"ARRYSCRN",VARPTR(ATOS(0))
  134. 30 PUTSCRN=VARPTR(ATOS(0)):LA=VARPTR(SCRN(0))
  135. 40 BLOAD"PIC1",LA
  136. 50 KEY OFF:CLS:SCREEN 1:COLOR 0,1
  137. 60 CALL PUTSCRN(SCRN(0)):BLOAD"PIC2",LA
  138. 70 IF INKEY$<>" " THEN 70
  139. 80 CALL PUTSCRN(SCRN(0)):BLOAD"PIC3",LA
  140. 90 IF INKEY$<>" " THEN 90
  141. 100 CALL PUTSCRN(SCRN(0))
  142. 110 IF INKEY$<>" " THEN 110
  143. 120 CLS:SCREEN 0:WIDTH 80:KEY ON:END
  144.  
  145.     Programs 4 and 5 show the source code for the SCRNARRY and ARRYSCRN
  146. subroutines.  They aren't required for use with Programs 1-3; they're
  147. provided so machine language programmers can observe the techniques
  148. involved.  An assembler is required to enter these listings.
  149.  
  150. Program 4: SCRNARRY Source Code
  151.  
  152. ; This subroutine copies 16192 bytes from the video display into a
  153. ; BASIC array.
  154.  
  155. CSEG    SEGMENT
  156. STOA    PROC    FAR
  157.         ASSUME CS:CSEG
  158.         PUSH    ES           ; Save extra segment
  159.         PUSH    DS           ; Set the extra segment
  160.         POP     ES           ;     equal to the data segment
  161.         PUSH    DS           ; Save the data segment
  162.         MOV     BP,SP        ; Make BP point to the stack
  163.         MOV     AX,0B800H    ; Set data segment to beginning
  164.         MOV     DS,AX        ;     of video RAM
  165.         MOV     CX,8096      ; Initialize move counter
  166.         XOR     SI,SI        ; Initialize source index
  167.         MOV     DI,8[BP]     ; Init destination index to array offset
  168.         CLD                  ; Set direction flag
  169. REP     MOVSW                ; Move the display to the array
  170.         POP     DS           ; Restore the data segment
  171.         POP     ES           ; Restore the extra segment
  172.         RET     2            ; Clean up the stack
  173. STOA    ENDP
  174. CSEG    ENDS
  175.         END
  176.  
  177. Program 5: ARRYSCRN Source Code
  178.  
  179. ; This subroutine copies 16192 bytes from a BASIC array to the video
  180. ; display.
  181.  
  182. CSEG    SEGMENT
  183. ATOS    PROC    FAR
  184.         ASSUME CS:CSEG
  185.         PUSH    ES           ; Save extra segment
  186.         MOV     BP,SP        ; Make BP point to stack
  187.         MOV     AX,0B800H    ; Set extra segment to beginning
  188.         MOV     ES,AX        ;     of video RAM
  189.         MOV     CX,8096      ; Initialize move counter
  190.         XOR     DI,DI        ; Initialize destination index
  191.         MOV     SI,6[BP]     ; Init source index to array offset
  192.         CLD                  ; Set direction flag
  193. REP     MOVSW                ; Move the array to the screen
  194.         POP     ES           ; Restore extra segment
  195.         RET     2            ; Clean up stack
  196. ATOS    ENDP
  197. CSEG    ENDS
  198.         END
  199.  
  200.